<?php
namespace addons\epay\controller;
class YunPayment 
{
    public $privateKeyPath = __DIR__.'/../certs/6888805123969.pfx';
    public $privateKeyPwd = '123456';
    
    public function dopayment($amount,$type,$method,$out_trade_no,$title,$notifyurl,$platform){
        if($platform=='App'){
            $product_code='02020004';
/*            $return_url=header("Location:sandh5payres://goHomePage");*/
            $return_url='';
        }else{
            $product_code='02020002';
            $return_url='https://haibeishichang.nfttop123.com';
        }
        $data = [
            'version' => 10,
            'mer_no' =>  '6888805123969', //商户号
          //  'mer_no' =>  '68888TS123969', //商户号
            'mer_order_no' => $out_trade_no, //商户唯一订单号
            'create_time' => date('YmdHis'),
            'expire_time' => date('YmdHis', time()+30*60),
            'order_amt' => $amount, //订单支付金额
            'notify_url' =>$notifyurl, //订单支付异步通知
            'return_url' =>$return_url , //订单前端页面跳转地址
            'create_ip' => "47_104_18_165",
            'goods_name' =>$title,
            'store_id' => '000000',
            'product_code' => $product_code,
 //  ○ 开户账户页面 product_code：00000001
 //  ○ 消费C2B product_code：04010001
 //  ○ 担保消费(C2C)  product_code：04010004
 //  ○ 消费（C2C） product_code：04010003
            'clear_cycle' => '3',
 //pay_extra参考语雀文档4.3
          //  'pay_extra' => json_encode(["userId"=>"12","nickName"=>"123","accountType"=>"1"]),
            'pay_extra' => "{}",
            'accsplit_flag' => 'NO',
            'jump_scheme' => 'sandcash://scpay',
            'meta_option' => json_encode([["s" => "Android","n" => "wxDemo","id" => "com.pay.paytypetest","sc" => "com.pay.paytypetest"]]),
            'sign_type' => 'RSA'
            
        ];

        $temp = $data;
        unset($temp['goods_name']);
        unset($temp['jump_scheme']);
        unset($temp['expire_time']);
        unset($temp['product_code']);
        unset($temp['clear_cycle']);
        unset($temp['meta_option']);
        
        file_put_contents('log.txt', date('Y-m-d H:i:s', time()) . " 签名串:" . $this->getSignContent($temp) . "\r\n", FILE_APPEND);
        
        // echo $this->getSignContent($temp);exit;
        $sign = $this->sign($this->getSignContent($temp));
        $data['sign'] = $sign;
      //  \think\Log::info($data);
        $query = http_build_query($data);
     //   \think\Log::info(1);
        if($platform=='App'){
    //        \think\Log::info(2);
            $payurl =json_encode($data);  // 电子钱包【云账户】：cloud
        }else{
            $payurl = "https://sandcash.mixienet.com.cn/pay/h5/alipay?".$query;
        }

       // \think\Log::info($payurl);
        return $payurl; // 返回支付url
    }

    function sign($str) {
     //   echo $this->privateKeyPath;exit;
        $file = file_get_contents($this->privateKeyPath);
        if (!$file) {
            throw new \Exception('loadPk12Cert::file
                    _get_contents');
        }
        if (!openssl_pkcs12_read($file, $cert, $this->privateKeyPwd)) {
            throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
        }
        $pem = $cert['pkey'];
        openssl_sign($str, $sign, $pem);
        $sign = base64_encode($sign);
        return $sign;
    }

    function getSignContent($params) {
        ksort($params);
    
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
    
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
    
        unset ($k, $v);
        return $stringToBeSigned;
    }
    
    function checkEmpty($value)
    {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }
}
/*$a = new YunPayment();
$b = $a->dopayment();
echo $b;*/